<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" class="fonts">
      <el-breadcrumb-item :to="{ path: '/repair' }">内容管理</el-breadcrumb-item>
      <el-breadcrumb-item>修理厂管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>
    <div style="width: 500px">
      <el-input
        v-model="input"
        placeholder="请输入要查询的修理厂店名"
        class="floatl"
        style="width: 300px"
      ></el-input>
      <el-button
        type="primary"
        class="floatl"
        style="margin-left: 30px"
        @click="inquire"
        >查询</el-button
      >
      <el-button
        type="primary"
        class="floatl"
        style="margin-left: 30px"
        @click="resets"
        >重置</el-button
      >
    </div>

    <div>
      <br />
      <br />
      <el-button type="primary" style="margin-top: 10px"
        ><router-link to="/addrepair">+ 添加修理厂</router-link></el-button
      >
      <el-button type="primary" @click="daochu">+ 导出数据</el-button>

      <!-- 导出表格预览 -->
      <el-dialog title="表格保存预览" width="70%" :visible.sync="selectWindow">
        <el-table :data="tableData" id="selectTable" height="380px">
          <el-table-column prop="id" label="编号"> </el-table-column>
          <el-table-column prop="name" label="店铺名称"> </el-table-column>

          <el-table-column prop="shortName" label="短名"> </el-table-column>
          <el-table-column prop="carBrandId" label="店铺主修品牌">
          </el-table-column>
          <el-table-column prop="discount" label="店铺折扣"> </el-table-column>
          <el-table-column prop="area" label="区域"> </el-table-column>
          <el-table-column prop="location" label="地址"> </el-table-column>
          <el-table-column prop="sign" label="是否签约"> </el-table-column>
        </el-table>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="exportExcel">确定保存</el-button>
        </div>
      </el-dialog>
    </div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="id" label="编号" width="60px" align="center" >
      </el-table-column>
      <el-table-column prop="name" label="店铺名称" width="80px" align="center">
      </el-table-column>
      <el-table-column prop="shortName" label="短名称" align="center">
      </el-table-column>
      <el-table-column prop="carBrandId" label="店铺主修品牌" align="center">
      </el-table-column>
      <el-table-column prop="discount" label="店铺折扣" align="center">
      </el-table-column>
      <el-table-column prop="area" label="区域" align="center">
      </el-table-column>
      <el-table-column prop="location" label="地址" align="center">
      </el-table-column>
      <el-table-column prop="sign" label="是否签约" align="center">
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template scope="scope" class="active">
          <el-button
            type="primary"
            @click="modification(scope.row)"
            icon="el-icon-edit"
            circle
          ></el-button>
          <el-button
            type="danger"
            @click="delBtn(scope.row)"
            icon="el-icon-delete"
            circle
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block" align="center">
      <el-pagination
        @current-change="handleCurrentChange"
        :page-size="pageSize"
        layout="total,  prev, pager, next, jumper"
        :total="count"
      >
      </el-pagination>
    </div>
  </div>
</template>
  </div>
</template>

<script>
import router from "../../router";
import htmlToExcel from "@/excel/htmlToExcel.js";
export default {
  data() {
    return {
      selectWindow: false,
      input: "",
      tableData: [],
      carid: "",
      count: 0,
      pageSize: 10,
      data: {
        limit: this.pageSize,
        page: 1,
      },
    };
  },
  methods: {
    resets() {
      this.input=''
      this.$axios({
      url: "/repairFactory/list",
      method: "POST",
      data: this.data,
    }).then((res) => {
      if (res.data.code === 0) {
        // console.log(res.data.data);
        this.tableData = res.data.data;
        // console.log(this.tableData)
        this.count = res.data.count;
        this.tableData.forEach((item) => {
          this.$axios({
            url: "/carBrand/find",
            method: "GET",
            params: {
              id: item.carBrandId,
            },
          }).then((res) => {
            // console.log(res.data.data.id);
            if (res.data.code === 0) {
              // console.log(item.carBrandId);
              if (item.carBrandId === res.data.data.id) {
                item.carBrandId = res.data.data.name;
                // console.log(this.tableData.carBrandId);
                // console.log(this.tableData);
              }
            }
          });
        });
      }
    });
    },
    //导出
    daochu() {
      if (this.tableData.length < 1) {
        this.$message.error("请选择要导出的内容！");
        return false;
      }
      this.selectWindow = true;
    },

    exportExcel() {
      htmlToExcel.getExcel("#selectTable", "修理厂列表");
    },
    //分页
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      let data = this.data;
      data.page = val;
      this.pagep(data);
    },

    //查询到所有数据渲染表格
    pagep() {
      this.$axios({
        url: "/repairFactory/list",
        method: "POST",
        data: this.data,
      }).then((res) => {
        console.log(res);
        if (res.data.code === 0) {
          this.tableData = res.data.data;
          this.count = res.data.count;
          console.log(this.count);
        }
      });
    },

    //删除
    delBtn(row) {
      console.log(row);
      this.$axios({
        url: "/repairFactory/delete",
        method: "POST",
        params: {
          id: row.id,
        },
      }).then((res) => {
        console.log(res);
        if (res.data.code === 0) {
          console.log("成功");
          this.$router.push({ path: "/repair" });
        }
      });
    },

    //修改
    modification(row) {
      console.log(row.id);
      //本地存储
      sessionStorage.setItem("id", JSON.stringify(row));
      // localStorage.setItem("id",row.id)
      this.$router.push({ path: "/amendrepair" });
      // this.$router.push({ path: "/amendfs" })
    },

    //查询
    inquire() {
      if (this.input == "") {
        return; //判断当前输入框是否为空
      }
      this.$axios({
        url: "/repairFactory/list",
        method: "POST",
        data: {
          name: this.input,
        },
      }).then((res) => {
        console.log(res);
        if (res.data.code === 0) {
          this.tableData = res.data.data;
          this.count = res.data.count;
        }
      });
    },
  },

  created() {
    // 查看所有修理厂

    this.$axios({
      url: "/repairFactory/list",
      method: "POST",
      data: this.data,
    }).then((res) => {
      if (res.data.code === 0) {
        // console.log(res.data.data);
        this.tableData = res.data.data;
        // console.log(this.tableData)
        this.count = res.data.count;
        this.tableData.forEach((item) => {
          this.$axios({
            url: "/carBrand/find",
            method: "GET",
            params: {
              id: item.carBrandId,
            },
          }).then((res) => {
            // console.log(res.data.data.id);
            if (res.data.code === 0) {
              // console.log(item.carBrandId);
              if (item.carBrandId === res.data.data.id) {
                item.carBrandId = res.data.data.name;
                // console.log(this.tableData.carBrandId);
                // console.log(this.tableData);
              }
            }
          });
        });
      }
    });
  },
};
</script>

<style scoped>
.floatl {
  float: left;
}
a {
  text-decoration: none;
  color: white;
}
.fonts {
  font-size: 20px;
}
</style>
